﻿/*
Copyright (c) 2015 Alex Doumanoglou, http://alexdoumanoglou.info

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using MonitorCore;

namespace MultiMonitorViewer
{
    class MultiMonitorViewer : ApplicationContext
    {
        #region Private Members
        private NotifyIcon m_NotifyIcon = new NotifyIcon();
        private ContextMenu m_ContextMenu = new ContextMenu();
        private ScreenCapturer m_ScreenCapturer = new ScreenCapturer();
        private FrmView m_FrmView;
        private FrmAbout m_FrmAbout;
        #endregion

        #region Private Methods
        private Icon GetAppIcon()
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            foreach (string resource in thisExe.GetManifestResourceNames())
            {
                if (resource.ToLowerInvariant().Contains("appicon"))
                {
                    return new Icon(thisExe.GetManifestResourceStream(resource));
                }
            }
            return null;
        }

        private void ShowView()
        {
            if (m_FrmView == null || m_FrmView.IsDisposed)
            {
                m_FrmView = new FrmView();
                m_FrmView.ScreenCapturer = m_ScreenCapturer;
                m_FrmView.Show();
            }
            else if (!m_FrmView.Visible)
                m_FrmView.Visible = true;

            m_FrmView.Activate();
        }

        private void ShowAbout()
        {
            if (m_FrmAbout == null || m_FrmAbout.IsDisposed)
            {
                m_FrmAbout = new FrmAbout();
                m_FrmAbout.Show();
            }
            else if (!m_FrmAbout.Visible)
                m_FrmAbout.Visible = true;

            m_FrmAbout.Activate();
        }

        #region Event Handlers

        private void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            ShowView();
        }
        
        private void OnSelectDisplayClick(object sender, EventArgs e)
        {
            MenuItem mItem = sender as MenuItem;
            m_ScreenCapturer.ActiveScreenIndex = (int)mItem.Tag;
            ShowView();
        }

        private void OnQuitButton_Clicked(object sender, EventArgs e)
        {
            m_NotifyIcon.Visible = false;
            if (m_FrmView != null && !m_FrmView.IsDisposed)
                m_FrmView.Close();
            m_ScreenCapturer.Dispose();
            Application.Exit();
        }

        private void OnAboutClick(object sender, EventArgs e)
        {
            ShowAbout();
        }

        #endregion

        #endregion

        #region Constructors
        public MultiMonitorViewer()
        {
            int devIndex = 0;
            m_ContextMenu.MenuItems.Add(new MenuItem("About...", OnAboutClick));            
            foreach (string screenName in m_ScreenCapturer.Screens)
            {
                MenuItem mItem = new MenuItem(screenName, new EventHandler(OnSelectDisplayClick));               
                mItem.Tag = devIndex++;
                m_ContextMenu.MenuItems.Add(mItem);
            }
            m_ContextMenu.MenuItems.Add(new MenuItem("Quit", new EventHandler(OnQuitButton_Clicked)));
            m_NotifyIcon.Icon = GetAppIcon();
            m_NotifyIcon.Text = "Multi-Monitor Viewer";
            m_NotifyIcon.DoubleClick += new EventHandler(NotifyIcon_DoubleClick);
            m_NotifyIcon.ContextMenu = m_ContextMenu;            
            m_NotifyIcon.Visible = true;
            m_NotifyIcon.ShowBalloonTip(5000, "Multi-Monitor Viewer", "Multi-Monitor Viewer is running in your system tray. Double click the tray icon to view any monitor's contents inside a window.", ToolTipIcon.Info);
        }      
        #endregion


    }
}
